package com.wwz.leetcode;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class LeetCode336 {

    public static void main(String[] args) {
        String[] words = {"bb", "bababab", "baab", "abaabaa", "aaba", "", "bbaa", "aba", "baa", "b"};
        List<List<Integer>> lists = palindromePairs(words);
        System.out.println(lists);
    }

    public static List<List<Integer>> palindromePairs(String[] words) {

        List<List<Integer>> result = new ArrayList<List<Integer>>();
        int z = 1;
        for (int i = 0; i < words.length - 1; i++) {
            for (int j = z; j <= words.length - 1; j++) {
                //如果有空
                if (words[i].equals("")) {
                    StringBuffer sb = new StringBuffer(words[j]);
                    StringBuffer r = sb.reverse();
                    if (r.toString().equals(words[j])) {
                        result.add(Arrays.asList(i, j));
                        result.add(Arrays.asList(j, i));
                    }
                    continue;
                }
                if (words[j].equals("")) {
                    StringBuffer sb = new StringBuffer(words[i]);
                    StringBuffer r = sb.reverse();
                    if (r.toString().equals(words[i])) {
                        result.add(Arrays.asList(i, j));
                        result.add(Arrays.asList(j, i));
                    }
                    continue;
                }
                //头尾相等 正拼
                char c = words[i].charAt(0);//头
                char c1 = words[j].charAt(words[j].length() - 1);//尾
                if (c == c1) {
                    String s = words[i] + words[j];
                    StringBuffer sb = new StringBuffer(s);
                    StringBuffer r = sb.reverse();
                    if (r.toString().equals(s)) {
                        result.add(Arrays.asList(i, j));
                    }
                }
                //尾头相等 反拼
                char c2 = words[i].charAt(words[i].length() - 1);
                char c3 = words[j].charAt(0);
                if (c2 == c3) {
                    String s2 = words[j] + words[i];
                    StringBuffer sb = new StringBuffer(s2);
                    StringBuffer r = sb.reverse();
                    if (r.toString().equals(s2)) {
                        result.add(Arrays.asList(j, i));
                    }
                }
            }
            z++;
        }
        return result;
    }
}
